/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
 var levelOrder = function(root) {
    let res = []
      let queue = [root]
      let flag = true
      while(root!=null&&queue.length){
          let len = queue.length; 
          let curLevel = []   
          while(len--){
              let node = queue.pop()
              if(flag){
                  curLevel.unshift(node.val)
              }else{                
                   curLevel.push(node.val) 
              }
                           
              node.right&&queue.unshift(node.right)
              node.left&&queue.unshift(node.left)                  
          }
          flag = !flag
          res.push(curLevel)
      }
      return res
  };